package com.supcon.exchangedata.service.gj;

import com.supcon.exchangedata.pojo.entity.Gps.Gps;
import com.supcon.exchangedata.pojo.entity.mysql.operation.hj.HjCleanGps;
import com.supcon.exchangedata.pojo.entity.oracle.CtGpsStatisitcInfo;
import com.supcon.exchangedata.pojo.entity.mysql.operation.gj.SgjjtBusGps;
import com.supcon.exchangedata.pojo.entity.oracle.CtGpsStatisitcInfoHis;
import com.supcon.exchangedata.repository.master.SlaveCarGpsInfo;
import com.supcon.exchangedata.repository.master.SlaveCarGpsInfoHis;
import com.supcon.exchangedata.repository.slave4.SlaveGjCarGpsInfo;
import com.supcon.exchangedata.utils.PositionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yangbin
 * @Date: 2022/12/23/16:07
 * @Description:
 */
@Service
@Slf4j
public class BusCarGpsService {
    @Resource
    private SlaveGjCarGpsInfo slaveGjCarGpsInfo;
    @Resource
    private SlaveCarGpsInfo slaveCarGpsInfo;
    @Resource
    private SlaveCarGpsInfoHis slaveCarGpsInfoHis;
    @Resource
    private RestTemplate restTemplate;

    public static final String GJURL = "https://jsczh.hzcjtz.com:8002/data_center/interface/common/use/100269";


    /**
     * 同步公交车点位
     */
    @Transactional
    public void syncBusCarGps() {
        List<CtGpsStatisitcInfo> list = new ArrayList<>();

        /*List<SgjjtBusGps> all = slaveGjCarGpsInfo.findCurrentBusCarStation();
        if (!CollectionUtils.isEmpty(all)) {


            DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            for (SgjjtBusGps hjCleanGps : all) {
                LocalDateTime bizTime = hjCleanGps.getBizTime();
                String times = timeFormatter.format(hjCleanGps.getRecdatetime());
                CtGpsStatisitcInfo gpsStatisitcInfo = new CtGpsStatisitcInfo();
                String carNumber = hjCleanGps.getPlate();
                gpsStatisitcInfo.setLicensePlateNum(carNumber);
                gpsStatisitcInfo.setType(1L);
                gpsStatisitcInfo.setCurrTime(times);
                gpsStatisitcInfo.setLayerCode("1009");
                String routeName = hjCleanGps.getRouteName();
                if (!StringUtils.isEmpty(routeName)){
                    gpsStatisitcInfo.setLineId(routeName);
                }
                if (!StringUtils.isEmpty(hjCleanGps.getSpeed())) {
                    try {
                        gpsStatisitcInfo.setSpeed(hjCleanGps.getSpeed());
                    } catch (Exception e) {
                        System.out.println("速度:" + hjCleanGps.getSpeed());
                    }

                }

                if (hjCleanGps.getLatitude() != null && hjCleanGps.getLatitude().compareTo(BigDecimal.ZERO)>0 &&
                        hjCleanGps.getLongitude() != null && hjCleanGps.getLongitude().compareTo(BigDecimal.ZERO)>0){
                    Gps gps = PositionUtil.gcj_To_Gps84(hjCleanGps.getLongitude().doubleValue(), hjCleanGps.getLatitude().doubleValue());
                    //log.info("转换后的数据: {}",gps);
                    gpsStatisitcInfo.setLatitude(Double.valueOf(gps.getWgLat()));
                    gpsStatisitcInfo.setLongitude(Double.valueOf(gps.getWgLon()));
                    list.add(gpsStatisitcInfo);
                }
                 BigDecimal oriLatitude = hjCleanGps.getLatitude();
                    BigDecimal oriLongitude = hjCleanGps.getLongitude();
                    if (oriLongitude != null && oriLatitude != null) {
                    gpsStatisitcInfo.setLatitude(oriLatitude.doubleValue());
                    gpsStatisitcInfo.setLongitude(oriLongitude.doubleValue());

                }
            }
        }
*/
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("key", "46bb94771c5149daa5977f718fda467a");
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        LinkedMultiValueMap<Object, Object> postParam = new LinkedMultiValueMap<>();
        HttpEntity<Object> request = new HttpEntity<>(postParam, httpHeaders);
        Map map = restTemplate.postForObject(GJURL, request, Map.class);
        List<Map> data = (List<Map>) map.get("data");
        if (!CollectionUtils.isEmpty(data)){
            try {
                slaveCarGpsInfo.deleteByTypeAndLayerCodeDiy(1L, "1009");
            } catch (Exception e) {

            }
        }
        data.forEach(v -> {
            CtGpsStatisitcInfo gpsCarStatisitcInfo = new CtGpsStatisitcInfo();
            //车牌号码
            Object plate = v.get("plate");
            if (null != plate) {
                gpsCarStatisitcInfo.setLicensePlateNum(plate.toString());
            }
            Object time = v.get("biz_time");
            if (null != time) {
                gpsCarStatisitcInfo.setCurrTime(time.toString());
            }
            Object speed = v.get("speed");
            if (null != speed) {
                Double s2 = Double.valueOf(speed.toString());
                //String s2 = speed.toString();
                gpsCarStatisitcInfo.setSpeed(BigDecimal.valueOf(s2));
            }
            Object name = v.get("route_name");
            if (null != name) {
                gpsCarStatisitcInfo.setLineId(name.toString());
            }
            Object clzbh = v.get("clzbh");
            if (null != clzbh) {
                gpsCarStatisitcInfo.setClzbh(clzbh.toString());
            }
            gpsCarStatisitcInfo.setLayerCode("1009");
            gpsCarStatisitcInfo.setType(1L);
            Object lat = v.get("latitude");
            Object lon = v.get("longitude");
            if (null != lon || null != lat) {
                String longitude = lon.toString();
                String latitude = lat.toString();
                Double longitude1 = Double.valueOf(longitude);
                Double latitude1 = Double.valueOf(latitude);
                if (BigDecimal.valueOf(longitude1).compareTo(BigDecimal.ZERO)>0 && BigDecimal.valueOf(latitude1).compareTo(BigDecimal.ZERO)>0){
                    Gps gps = PositionUtil.gcj_To_Gps84(longitude1.doubleValue(),latitude1.doubleValue());
                    gpsCarStatisitcInfo.setLatitude(Double.valueOf(gps.getWgLat()));
                    gpsCarStatisitcInfo.setLongitude(Double.valueOf(gps.getWgLon()));
                    list.add(gpsCarStatisitcInfo);
                }
            }
        });
        if (!CollectionUtils.isEmpty(list)) {
            slaveCarGpsInfo.saveAll(list);
        }
    }

    /**
     * 同步公交车轨迹历史数据
     */
    @Transactional
    public void gjHis(){
        List<CtGpsStatisitcInfoHis> objects = new ArrayList<>();
/*        List<CtGpsStatisitcInfo> byTypeAndAndLayerCode = slaveCarGpsInfo.findByTypeAndAndLayerCode(1L, "1009");
        byTypeAndAndLayerCode.forEach(v->{
            CtGpsStatisitcInfoHis ctGpsStatisitcInfoHis = new CtGpsStatisitcInfoHis();
            BeanUtils.copyProperties(v,ctGpsStatisitcInfoHis);
            objects.add(ctGpsStatisitcInfoHis);
        });*/
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("key", "46bb94771c5149daa5977f718fda467a");
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        LinkedMultiValueMap<Object, Object> postParam = new LinkedMultiValueMap<>();
        HttpEntity<Object> request = new HttpEntity<>(postParam, httpHeaders);
        Map map = restTemplate.postForObject(GJURL, request, Map.class);
        List<Map> data = (List<Map>) map.get("data");
        int s= 1;
        data.forEach(v -> {
            CtGpsStatisitcInfoHis gpsCarStatisitcInfo = new CtGpsStatisitcInfoHis();
            //车牌号码
            Object plate = v.get("plate");
            if (null != plate) {
                gpsCarStatisitcInfo.setLicensePlateNum(plate.toString());
            }
            Object time = v.get("biz_time");
            if (null != time) {
                gpsCarStatisitcInfo.setCurrTime(time.toString());
            }
            Object speed = v.get("speed");
            if (null != speed) {
                Double s2 = Double.valueOf(speed.toString());
                //String s2 = speed.toString();
                gpsCarStatisitcInfo.setSpeed(BigDecimal.valueOf(s2));
            }
            Object name = v.get("route_name");
            if (null != name) {
                gpsCarStatisitcInfo.setLineId(name.toString());
            }
            gpsCarStatisitcInfo.setLayerCode("1009");
            gpsCarStatisitcInfo.setType(1L);
            Object lat = v.get("latitude");
            Object lon = v.get("longitude");
            if (null != lon && null != lat){
                String longitude = lon.toString();
                String latitude = lat.toString();
                Double longitude1 = Double.valueOf(longitude);
                Double latitude1 = Double.valueOf(latitude);
                if (BigDecimal.valueOf(longitude1).compareTo(BigDecimal.ZERO)>0 && BigDecimal.valueOf(latitude1).compareTo(BigDecimal.ZERO)>0){
                    Gps gps = PositionUtil.gcj_To_Gps84(longitude1.doubleValue(),latitude1.doubleValue());
                    gpsCarStatisitcInfo.setLatitude(Double.valueOf(gps.getWgLat()));
                    gpsCarStatisitcInfo.setLongitude(Double.valueOf(gps.getWgLon()));
                    objects.add(gpsCarStatisitcInfo);
                }
            }
        });
        slaveCarGpsInfoHis.saveAll(objects);
    }

    public void newSyncBusCarGps(){
        List<CtGpsStatisitcInfoHis> list = new ArrayList<>();
        Long aLong = slaveGjCarGpsInfo.maxTongId().getTongid();
        if (aLong != null){
            CtGpsStatisitcInfoHis one = slaveCarGpsInfoHis.findByTongId(aLong.toString());
            if (null == one){
                List<SgjjtBusGps> all = slaveGjCarGpsInfo.findAll();
                if (!CollectionUtils.isEmpty(all)) {
                    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    for (SgjjtBusGps hjCleanGps : all) {
                        LocalDateTime bizTime = hjCleanGps.getBizTime();
                        String times = timeFormatter.format(bizTime);
                        CtGpsStatisitcInfoHis gpsStatisitcInfo = new CtGpsStatisitcInfoHis();
                        String carNumber = hjCleanGps.getPlate();
                        gpsStatisitcInfo.setLicensePlateNum(carNumber);
                        gpsStatisitcInfo.setType(1L);
                        gpsStatisitcInfo.setCurrTime(times);
                        gpsStatisitcInfo.setLayerCode("1009");
                        gpsStatisitcInfo.setTongId(hjCleanGps.getTongid().toString());
                        if (!StringUtils.isEmpty(hjCleanGps.getSpeed())) {
                            try {
                                gpsStatisitcInfo.setSpeed(hjCleanGps.getSpeed());
                            }catch (Exception e){
                                System.out.println("速度:"+hjCleanGps.getSpeed());
                            }

                        }
                        String routeName = hjCleanGps.getRouteName();
                        if (!StringUtils.isEmpty(routeName)){
                            gpsStatisitcInfo.setLineId(routeName);
                        }
/*                        String oriLatitude = hjCleanGps.getLatitude();
                        String oriLongitude = hjCleanGps.getLongitude();
                        if (!StringUtils.isEmpty(oriLatitude) && !StringUtils.isEmpty(oriLongitude)) {
                            Gps gps = PositionUtil.gcj_To_Gps84(Double.valueOf(oriLatitude), Double.valueOf(oriLongitude));
                            gpsStatisitcInfo.setLatitude(Double.valueOf(gps.getWgLat()));
                            gpsStatisitcInfo.setLongitude(Double.valueOf(gps.getWgLon()));
                            list.add(gpsStatisitcInfo);
                        }*/
                        if (hjCleanGps.getLatitude() != null && hjCleanGps.getLatitude().compareTo(BigDecimal.ZERO)>0 &&
                                hjCleanGps.getLongitude() != null && hjCleanGps.getLongitude().compareTo(BigDecimal.ZERO)>0){
                            Gps gps = PositionUtil.gcj_To_Gps84(hjCleanGps.getLongitude().doubleValue(), hjCleanGps.getLatitude().doubleValue());
                            //log.info("转换后的数据: {}",gps);
                            gpsStatisitcInfo.setLatitude(Double.valueOf(gps.getWgLat()));
                            gpsStatisitcInfo.setLongitude(Double.valueOf(gps.getWgLon()));
                            list.add(gpsStatisitcInfo);
                        }
                    }
                }
                if(!CollectionUtils.isEmpty(list)){
                    slaveCarGpsInfoHis.saveAll(list);
                }
            }
        }
    }
}
